<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/1
 * Time: 13:37
 */
namespace app\admin\controller;

use think\Db;
use think\Controller;
use app\admin\model\Authrule as authruleModel;
class Authrule extends Controller
{
    //前置操作
    protected $beforeActionList = [
        //'first',
        //'second' =>  ['except'=>'hello'],
        //'delsoncate'  =>  ['only'=>'del'],
    ];

    /*
     *  权限管理列表
     */
    public function authrule()
    {
        $authrule=new authruleModel();
        if (request()->isPost()){
            $sorts=input('post.');
            foreach ($sorts as $k => $v){
                $authrule->update(['id'=>$k,'sort'=>$v]);
            }
            $this->redirect('Authrule/authrule');
            return;
        }
        $authruleres=$authrule->authruleTree();
        $this->assign('authruleRes',$authruleres);
        return view();
    }


    /*
     * 权限管理添加
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            //验证   在控制器中的编码格式
            $validate = \think\Loader::validate('Authrule');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $plevel=Db::table('zq_auth_rule')->where('id',$data['pid'])->field('level')->find();   //plevel 顶级级别
            if ($plevel){     //如果添加的顶级权限有数据的话
                $data['level']=$plevel['level']+1;  //那就说明并非顶级权限
            }else{
                $data['level']=0;  //那就说明是顶级权限
            }
            $add = Db::table('zq_auth_rule')->insert($data);
            if ($add){
               $this->success('添加成功','Authrule/authrule');
            } else {
                $this->error('添加权限失败');
            }
            return;
        }
        $authrule=new authruleModel();
        $authruleres=$authrule->authruleTree();
        $this->assign('authruleRes',$authruleres);
        return view();
    }

    /*
     *  权限管理编辑
     */
    public function edit()
    {
        if (request()->isPost()){
            $data=input('post.');
            //验证   在控制器中的编码格式
            $validate = \think\Loader::validate('Authrule');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $plevel=Db::table('zq_auth_rule')->where('id',$data['pid'])->field('level')->find();   //plevel 顶级级别
            if ($plevel){     //如果添加的顶级权限有数据的话
                $data['level']=$plevel['level']+1;  //那就说明并非顶级权限
            }else{
                $data['level']=0;  //那就说明是顶级权限
            }
            $save=Db::table('zq_auth_rule')->update($data);
            if ($save){
                $this->success('修改成功','Authrule/authrule');
            }else{
                $this->error('修改权限失败');
            }
            return;
        }
        $authrule=new authruleModel();
        $authruleres=$authrule->authruleTree();
        $authrules=Db::table('zq_auth_rule')->where(array('id'=>input('id')))->find();
        $this->assign(array(
            'authruleRes'=>$authruleres,
            'authrules'=>$authrules,
        ));
        return view();
    }


    /*
     *  权限管理删除
     */
    public function del()
    {
          $id = input('id');
		if (Db::table('zq_auth_rule')->where(['pid' => $id])->count()) {
            $this->error('该权限下存在子分类，请先删除');
        }
		$del=Db::table('zq_auth_rule')->delete($id);
        if ($del){
           $this->success('删除成功','Authrule/authrule');
        }else{
            $this->error('删除权限失败');
        }
    }
    //前置操作的方法
    public function delsoncate(){
        $authruleid=input('id');  //要删除当前栏目的id
        $authrule=new authruleModel();
        $authrule->getparentid($authruleid);
        $sonids=$authrule->getchildrenid($authruleid);
        if ($sonids){
            db('authrule')->delete($sonids);
        }
    }
 

}
